Redirect Compilation Notices

[ Start > PikeDevel > HowTo > Redirect Compilation Notices ] [ Edit this Page | Show Page Versions | Show Formatted ]


By default, Pike prints compilation warnings and errors to __stderr__. If you're writing a program that compiles code on the fly, it's often useful to "capture" compilation warnings and errors in order to present them in a more useful manner.

It is possible to provide the master object with a compiler error handler object that will keep track of errors and warnings. Once passed to the error handler, you can do other things with them, like log them or present a dialog box.

{code}

  object ee = ErrorContainer();
  master()->set_inhibit_compile_errors(ee);

  // we use a catch here because the compile operation could
  // throw an error that isn't due to syntax.
  mixed err = catch {
    res = ((program)"mycode")(); // could be a compile_* instead
  };

  master()->set_inhibit_compile_errors(0);

  if(strlen(ee->get())) {
    werror("error:\n%s\n", ee->get());
    exit(1);
  } else if(err) {
    werror(describe_backtrace(err));
    exit(1);
  }
  ee->print_warnings("compilation warnings:");



{code}

{code}
//!
class ErrorContainer
{
string d;
string errors="", warnings="";
string get()
{
  return errors;
}

//!
string get_warnings()
{
  return warnings;
}

//!
void print_warnings(string prefix) {
  if(warnings && strlen(warnings))
    werror(prefix+"\n"+warnings);
}

//!
void got_error(string file, int line, string err, int|void is_warning)
{
  if (file[..sizeof(d)-1] == d) {
    file = file[sizeof(d)..];
  }
  
  if( is_warning)
    warnings+= sprintf("%s:%s\t%s\n", file, line ? (string) line : "-", err);
  else
    errors += sprintf("%s:%s\t%s\n", file, line ? (string) line : "-", err);
}

//!
void compile_error(string file, int line, string err)
{
  got_error(file, line, "Error: " + err);
}

//!
void compile_warning(string file, int line, string err)
{
  got_error(file, line, "Warning: " + err, 1);
}

//!
void create()
{
  d = getcwd();
  if (sizeof(d) && (d[-1] != '/') && (d[-1] != '\\'))
    d += "/";
  
}
{code}

Powered by PikeWiki2

 
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University